/* ========================================================= */
/*          PSM - ACCEPTED/REJECTED BORROWERS                */
/* ========================================================= */

/* Import data */
clear all

*********************************************************************************
*
*	DESCRIPTION: This script applies different matching techniques to the dataset.
*                The package "psmatch2" (ssc install psmatch2) is required.
*
********************************************************************************


/* Start log file */
capture log close
log using "$logs\PSM_rejected.log", replace

/* ========================================================= */
/*                  LOAD & PREPARE DATA                      */
/* ========================================================= */
use "$output\rejected_agg_industries_modified.dta", clear

/* Keep only observations within the specified date range */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Aggregate loan variable */
gen loan = stloan + ltloan + bm + bi + oc

/* Log transformation */
foreach vari in loan {
    replace `vari' = log(1 + `vari')
}

/* Create dummy variables for missing values */
foreach var of varlist r_collateral_filled r13filled logassets_filled r3filled r5filled { 
    gen m_`var' = `var' == .
    replace `var' = 0 if `var' == .
}

/* ========================================================= */
/*               CONSTANT FIRM CHARACTERISTICS               */
/* ========================================================= */
preserve
    keep if delta < 0
    collapse (sum) co, by(siren)
    gen codummy = co > 0
    keep siren codummy
    save "$output\psm_co_rejected_dummy.dta", replace
restore

/* Prepare dataset for PSM */
preserve
    keep if delta == -11
    keep siren outloanyear size rating industry r_collateral_filled outsideloan ///
         ageatevent m_r_collateral_filled r13filled m_r13filled logassets_filled ///
         m_logassets_filled r3filled m_r3filled r5filled m_r5filled
    duplicates drop siren, force
    merge 1:1 siren using "$output\psm_co_rejected_dummy.dta"
    keep if _merge == 3
    drop _merge
    save "$output\psm_stable_rejected_keep.dta", replace
restore

/* ========================================================= */
/*               PREPARE FOR NEAREST NEIGHBOR MATCHING       */
/* ========================================================= */
drop datemonth
keep if delta <= 0 & delta >= -6
gen delta2 = delta + 6
drop delta
duplicates drop siren delta2, force

keep siren accepted delta2 $celllist $ylist

/* Reshape dataset */
qui reshape wide $ylist, i(siren accepted $celllist) j(delta2)

duplicates drop siren, force

merge 1:m siren using "$output\psm_stable_rejected_keep.dta"
keep if _merge == 3
drop _merge

/* ========================================================= */
/*                  DEFINE MATCHING VARIABLES                */
/* ========================================================= */
global xlist "i.outloanyear i.size i.codummy ageatevent i.rating ///
              r3filled i.m_r3filled r13filled i.m_r13filled ///
              i.m_r_collateral_filled r_collateral_filled ///
              logassets_filled i.m_logassets_filled"

global ylist "loan"    // Time-variant matching variables
global celllist "industry"

/* ========================================================= */
/*                  PROPENSITY SCORE MATCHING               */
/* ========================================================= */

/* ---------------------------------- */
/* 1. Propensity Score Matching (1:1) */
/* ---------------------------------- */
preserve
    capture drop _*
    capture graph drop _all

    foreach name in pscore support weight id n1 nn {
        gen `name' = .
    }

    egen cell = group($celllist)
    qui levels cell, local(gr)
    foreach i of local gr {
        capture noisily psmatch2 accepted loan0 loan1 loan2 loan3 loan4 loan5 $xlist ///
            if cell == `i', out(loan5) noreplacement common 

        if c(rc) == 0 {
            display "Processing cell `i'"
            foreach var of varlist pscore support weight id n1 nn {
                replace `var' = _`var' if cell == `i'
            }
        }
        else {
            display "Skipping cell `i' due to insufficient observations"
        }
    }

    keep siren pscore support weight id n1 nn
    keep if weight ~= .

    merge 1:m siren using "$output\rejected_agg_industries_modified.dta"
    keep if _merge == 3
    drop _merge

    save "$output\matched_sample_ps_norep_rejected.dta", replace
restore

/* ----------------------------------- */
/* 2. Propensity Score Matching (1:n)  */
/* ----------------------------------- */
preserve
    capture drop _*
    capture graph drop _all

    foreach name in pscore support weight id n1 nn {
        gen `name' = .
    }

    egen cell = group($celllist)
    qui levels cell, local(gr)
    foreach i of local gr {
        capture psmatch2 accepted loan0 loan1 loan2 loan3 loan4 loan5 $xlist ///
            if cell == `i', out(loan5) common 

        if c(rc) == 0 {
            display "Processing cell `i'"
            foreach var of varlist pscore support weight id n1 nn {
                replace `var' = _`var' if cell == `i'
            }
            capture psgraph, name(cell`i',replace) bin(30)
            capture pstest loan0 loan1 loan2 loan3 loan4 loan5 $xlist
        }
        else {
            display "Skipping cell `i' due to insufficient observations"
        }
    }

    keep siren pscore support weight id n1 nn 
    keep if weight ~= .

    sum pscore support weight id n1 nn, d
    merge 1:m siren using "$output\rejected_agg_industries_modified.dta"
    keep if _merge == 3
    drop _merge

    save "$output\matched_sample_ps_rep_industry_year_rejected.dta", replace
restore

/* -------------------------------- */
/* 3. Propensity Score Matching (n=5) */
/* -------------------------------- */
capture drop _*
foreach name in pscore support weight id n1 n2 n3 n4 n5 nn {
    gen `name' = .
}

egen cell = group($celllist)
qui levels cell, local(gr)
foreach i of local gr {
    capture psmatch2 accepted loan0 loan1 loan2 loan3 loan4 loan5 $xlist ///
        if cell == `i', out(loan6) neighbor(5) common 

    if c(rc) == 0 {
        display "Processing cell `i'"
        foreach var of varlist pscore support weight id n1-n5 nn {
            replace `var' = _`var' if cell == `i'
        }
    }
    else {
        display "Skipping cell `i' due to insufficient observations"
    }
}

keep siren pscore support weight id n1-n5 nn
keep if weight ~= .

merge 1:m siren using "$output\rejected_agg_industries_modified.dta"
keep if _merge == 3
drop _merge

save "$output\rejected_psm.dta", replace

/* Close log */
log close
